<?php

namespace App\Admin\Controllers;

use App\Admin\Grid\NGrid;
use App\Admin\Tools\Blacklist\BlacklistRemarkTool;
use App\Admin\Tools\BlacklistCreateTool;
use App\Models\BlacklistModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Grid\Tools;
use Illuminate\Support\Facades\Auth;

class BlacklistController extends AdminController
{
    protected $title = '黑名单';

    protected function grid(): NGrid
    {
        $grid = new NGrid(new BlacklistModel);
        $grid->model()
            ->leftJoin('admin_users', 'admin_users.id', 'blacklist.follow_user_id')
            ->leftJoin('admin_users as operator', 'operator.id', 'blacklist.operator_id')
            ->orderByDesc('blacklist.id')
            ->select([
                'blacklist.*',
                'admin_users.name as follow_name',
                'operator.name as operator_name',
            ]);
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->like('blacklist-name', '客户名');
                $filter->equal('blacklist-wechat', __('l.wechat'));
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('blacklist-mobile', __('l.mobile'));
            });
        });
//        if (!ISADMIN) {
//            if (ISMAIN) $grid->model()->where('blacklist.main_id', USERID);
//            else $grid->model()->where('blacklist.operator_id', USERID);
//        }
        if (!ISADMIN) {
            if (in_array(Auth::user()->roles->first()->slug, ['boss', 'finance'])) $grid->model()->where('blacklist.main_id', MAINID);
            else $grid->model()->where('blacklist.operator_id', USERID);
        }
        if (Auth::user()->roles->first()->slug == 'Administrator') $grid->column('id', 'ID')->sortable();
        $grid->column('name', '客户名称');
        $grid->column('gender', __('l.gender'))->display(function () {
            return $this->gender ? '男' : '女';
        });
        $grid->column('age', __('l.age'));
        $grid->column('area', __('task.area'));
        $grid->column('mobile', __('l.mobile'));
        $grid->column('wechat', __('l.wechat'));
        $grid->column('remark', __('l.remark'))->display(function () {
            if (mb_strlen($this->remark) < 16) return $this->remark;
            else return view('tooltip ', [
                'data' => $this->remark
            ]);
        });
        $grid->column('status', __('l.status'))->using(__('task.followStatusArr'));
        $grid->column('follow_name', __('task.followUser'));
        $grid->column('operator_name', '操作人');
        $grid->actions(function (Actions $action) {
            $action->disableView();
            $action->disableEdit();
            $action->disableDelete();
            $action->append(new BlacklistRemarkTool(
                $action->row->id,
                $action->row->name,
                json_decode($action->row->extends, true),
            ));
        });

        $grid->disableActions(false);
        $grid->disableCreateButton();
        $grid->tools(function (Tools $tools) {
            $tools->append(new BlacklistCreateTool);
        });
        return $grid;
    }

    public function form()
    {
        $form = new Form(new BlacklistModel);
        $form->column(1 / 2, function (Form $form) {
            $form->text('name', '客户姓名')->rules('required|min:0');
            $form->select('gender', __('l.gender'))->options(__('l.genderArr'))->rules('required|min:0');
            $form->text('age', __('l.gender'))->rules('required|min:0');
            $form->text('area', __('task.area'));
            $form->text('mobile', __('l.mobile'))->rules('required|regex:/^1[3-9]\d{9}$/');
            $form->text('wechat', __('l.wechat'));
            $form->textarea('remark', __('l.remark'));
        });
        return $form;
    }
}
